package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.activity.*;         //*相当于引入所有声明

public class MainActivityA extends AppCompatActivity {

    Button button_A;
    ActivityResultLauncher<Intent> launcher;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_a);

        textView = findViewById(R.id.textViewA2);       //对应40行和45行的传值

        launcher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                new ActivityResultCallback<ActivityResult>() {
                    @Override
                    public void onActivityResult(ActivityResult result) {
                        if (result.getResultCode()==666) {
                            String str1 = result.getData().getStringExtra("result1");
                            textView.setText(str1);     //显示str1的内容
                        }

                        if (result.getResultCode()==777) {
                            String str2 = result.getData().getStringExtra("result2");
                            textView.setText(str2);
                        }
                    }
                }
        );

        button_A = findViewById(R.id.buttonA1);
        button_A.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivityA.this,MainActivityB.class);     //MainActivityA用于说明this所处位置
                intent.putExtra("name", "king");
                //startActivity(intent);

                launcher.launch(intent);

            }
        });


    }
}